<?php
include dirname(__FILE__).'/../config.php';
include dirname(__FILE__).'/xiaobai.php';
function sendmassage($token,$tofakeid,$contant,$cookie,$cookies)
	{
		$siurl = $_SERVER['HTTP_HOST'];
		    $url = "http://xbsslcurl.sinaapp.com/cjreplay.php?siurl=$siurl&token=$token";
	$post_data= "tofakeid=$tofakeid&contant=$contant&cookie=$cookie&cookies=$cookies";
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36");
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION,true);
	$output = curl_exec($ch);
 //var_dump($output);//全部消息
	curl_close($ch);
	return $output;
}

function https_post($url,$data)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	$result=curl_exec($ch);
	if(curl_errno($ch))
	{
	return 'Error'.curl_error($ch);
	}
	curl_close($ch);
	return $result;
}

function kefu($openid,$content,$appid,$secret)
{
	$now=time();
	$sql="select * from weixin_token";
	$query=mysql_query($sql);
	$q=mysql_fetch_row($query);
	var_dump($q);
	if(!$q)
	{
		$guoqi=$now+15;
		$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
		$acc_json=file_get_contents($url);
		$acc_array=json_decode($acc_json,true);
		$token=$acc_array['access_token'];
		$in="insert into `weixin_token` values('".$acc_array['access_token']."','".$guoqi."')";
		mysql_query($in);
	}else if($q[1]<$now)
	{
		$guoqi=$now+15;
		$url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$appid.'&secret='.$secret;
		$acc_json=file_get_contents($url);
		$acc_array=json_decode($acc_json,true);
		$token=$acc_array['access_token'];
		$up="update weixin_token set token = '".$acc_array['access_token']."',date=".$guoqi;
		mysql_query($up);
	}
	else
	{
	$token=$q[0];
	}
	var_dump($token);
	$txt='{
		"touser":"'.$openid.'",
		"msgtype":"text",
		"text":
		{
			 "content":"'.$content.'"
		}
	}';
	$post_url='https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token='.$token;
	$result=https_post($post_url,$txt);
	var_dump($result);
}

$appid='wx92e9594aa61a61e8';
$secret='459a01135bf3a21c8c32ee01d48d7cdb';
echo kefu('oFrCKsxBRGYlFeVMcMOfbb3vcXFs','sadjaslkjdlkasjdlkasdj',$appid,$secret);
?>